#include "primaryplay.h"

PrimaryPlay::PrimaryPlay(int index)
{

    this->level=index;

    int array1[4][4] = { {0, 1, 0, 0},
                         {1, 1, 1, 0},
                         {0, 1, 0, 0},
                         {0, 0, 0, 0}} ;
    int array2[4][4] = { {1, 0, 1, 1},
                         {0, 0, 1, 1},
                         {1, 1, 0, 0},
                         {1, 1, 0, 1}} ;
    int array3[4][4] = {  {0, 0, 0, 0},
                          {0, 1, 1, 0},
                          {0, 1, 1, 0},
                          {0, 0, 0, 0}} ;
    int array4[4][4] = {  {0, 0, 0, 0},
                          {0, 1, 1, 0},
                          {0, 1, 1, 0},
                          {0, 0, 0, 0}} ;
    int array5[4][4] = {  {0, 0, 0, 0},
                          {0, 1, 1, 0},
                          {0, 1, 1, 0},
                          {0, 0, 0, 0}} ;
    int array6[4][4] = {  {0, 0, 0, 0},
                          {0, 1, 1, 0},
                          {0, 1, 1, 0},
                          {0, 0, 0, 0}} ;
    int array7[4][4] = {  {0, 0, 0, 0},
                          {0, 1, 1, 0},
                          {0, 1, 1, 0},
                          {0, 0, 0, 0}} ;
    int array8[4][4] = {  {0, 0, 0, 0},
                          {0, 1, 1, 0},
                          {0, 1, 1, 0},
                          {0, 0, 0, 0}} ;

    //设置窗口
    //固定尺寸
    this->setFixedSize(mainSceneWidth,mainSceneHeight);
    //标题
    this->setWindowTitle(mainSceneName);
    //图标
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    //创建菜单
    QMenuBar * bar = menuBar();
    this->setMenuBar(bar);
    QMenu* startMenu = bar->addMenu("开始");
    QAction * quitAction = startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //返回按钮
    PushButton_01 * back =new PushButton_01(":/res/BackButton.png",":/res/BackButtonSelected.png");
    back->setParent(this);
    back->move(this->width() - back->width(),this->height() - back->height());

    connect(back,&PushButton_01::clicked,[=](){

        QTimer::singleShot(500,this,[=](){
            this->hide();
            emit this->PrimarySceneBack();
        });
    });

    //当前关卡标题
    QLabel * label = new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    label->setFont(font);
    QString str = QString("Level: %1").arg(this->level);
    label->setText(str);
    label->setGeometry(QRect(30,this->height() - 50, this->width(),50 ));
    label->setAttribute(Qt::WA_TransparentForMouseEvents);

    //当前步数
    QLabel * plabel = new QLabel;
    plabel->setParent(this);
    QFont pfont;
    pfont.setFamily("华文新魏");
    pfont.setPointSize(20);
    plabel->setFont(pfont);
    QString pstr = QString("pace: %1").arg(this->pace);
    plabel->setText(pstr);
    plabel->setGeometry(QRect(30,this->height() - 100, this->width(),50 ));
    plabel->setAttribute(Qt::WA_TransparentForMouseEvents);

    //胜利图片
    QLabel * winLabel = new QLabel;
    winLabel->setParent(this);
    QPixmap pix;
    pix.load(":/res/xbdnhny.png");
    winLabel->setPixmap(pix);
    winLabel->setGeometry(0,0,pix.width(),pix.height());
    winLabel->move(this->width()*0.5-pix.width()*0.5,-pix.height());



    //初始化二维数组
    switch (this->level) {
    case 1:
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                this->gameArray[i][j] = array1[i][j];
            }
        }
        break;
    case 2:
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                this->gameArray[i][j] = array2[i][j];
            }
        }
        break;
    case 3:
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                this->gameArray[i][j] = array3[i][j];
            }
        }
        break;
    case 4:
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                this->gameArray[i][j] = array4[i][j];
            }
        }
        break;
    case 5:
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                this->gameArray[i][j] = array5[i][j];
            }
        }
        break;
    case 6:
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                this->gameArray[i][j] = array6[i][j];
            }
        }
        break;
    case 7:
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                this->gameArray[i][j] = array7[i][j];
            }
        }
        break;
    case 8:
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                this->gameArray[i][j] = array8[i][j];
            }
        }
        break;
    default:
        break;
    }

    //创建金币的背景图片
    for(int i = 0 ; i < 4;i++)
    {
       for(int j = 0 ; j < 4; j++)
       {

           //绘制背景图片
            QLabel* label = new QLabel;
            QPixmap pix;
            pix.load(":/res/BoardNode(1).png");
            label->setGeometry(0,0,pix.width(),pix.height());
            label->setPixmap(pix);
            label->setParent(this);
            label->move(307 + i*100,400+j*100);

            //创建金币
            QString str;
            if(this->gameArray[i][j] == 1){
                str = ":/res/Coin0001.png";
            }
            else
            {
                str = ":/res/Coin0008.png";
            }
            MyCoin * coin = new MyCoin(str);
            coin->setParent(this);
            coin->move(307 + i*100,400+j*100);
            coin->posX=i;
            coin->posY=j;
            coin->flag = gameArray[i][j];

            this->coinBtn[i][j]=coin;
            //点击硬币调用
            connect(coin,&MyCoin::clicked,[=](){

               coin->changeFlag();

               //同步二维数组，方便后期扩展
               gameArray[i][j]=gameArray[i][j]==0?1:0;

               //翻转周围硬币
               QTimer::singleShot(300,this,[=](){
                   //右侧
                   if(coin->posX+1<=3)
                   {
                       this->coinBtn[coin->posX+1][coin->posY]->changeFlag();
                       gameArray[coin->posX+1][coin->posY] = gameArray[coin->posX+1][coin->posY]==0?1:0;
                   }
                   //左侧
                   if(coin->posX-1>=0)
                   {
                       this->coinBtn[coin->posX-1][coin->posY]->changeFlag();
                       gameArray[coin->posX-1][coin->posY] = gameArray[coin->posX-1][coin->posY]==0?1:0;
                   }
                   //下侧
                   if(coin->posY+1<=3)
                   {
                       this->coinBtn[coin->posX][coin->posY+1]->changeFlag();
                       gameArray[coin->posX][coin->posY+1] = gameArray[coin->posX][coin->posY+1]==0?1:0;
                   }
                   //上侧
                   if(coin->posY-1>=0)
                   {
                       this->coinBtn[coin->posX][coin->posY-1]->changeFlag();
                       gameArray[coin->posX][coin->posY-1] = gameArray[coin->posX][coin->posY-1]==0?1:0;
                   }

                   //刷新步数显示
                   pace++;
                   QString pstr = QString("pace: %1").arg(this->pace);
                   plabel->setText(pstr);
                   plabel->setGeometry(QRect(30,this->height() - 100, this->width(),50 ));
                   plabel->setAttribute(Qt::WA_TransparentForMouseEvents);

                   //检测游戏是否胜利
                   this->isWin = true;
                   for(int i=0;i<4;i++)
                   {
                       for(int j=0;j<4;j++)
                       {
                           if(this->coinBtn[i][j]->flag==false)
                           {
                               this->isWin=false;
                               break;
                           }
                       }

                   }

                   if(this->isWin)
                   {
                       QPropertyAnimation *animation = new QPropertyAnimation(winLabel,"geometry");
                       animation->setDuration(1000);
                       animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                       animation->setEndValue(QRect(winLabel->x(),winLabel->y()+814,winLabel->width(),winLabel->height()));
                       animation->setEasingCurve(QEasingCurve::OutBounce);
                       animation->start(QAbstractAnimation::DeleteWhenStopped);
                       for(int i=0;i<4;i++)
                       {
                           for(int j=0;j<4;j++)
                           {
                               this->coinBtn[i][j]->isWin=true;
                           }
                       }
                   }
                   else
                   {
                       for(int i=0;i<4;i++)
                       {
                           for(int j=0;j<4;j++)
                           {
                               this->coinBtn[i][j]->isWin = false;
                           }
                       }
                   }
               });
            });
       }
    }

}
//绘图事件
void PrimaryPlay::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/bfda0be809541c57e1407952d1e6042.jpg");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //标题
    pix.load(":/res/Title.png");
    painter.drawPixmap((this->width()-pix.width())*0.5,30,pix);
}
